package com.zz.yt.test.delegate.net;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.constants.UserConstant;
import com.whf.android.jar.net.RestClient;
import com.whf.android.jar.util.FormatUtil;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.util.storage.LattePreference;
import com.zz.yt.test.R;

/**
 * Company:中再云图技术有限公司
 *
 * @author qf
 * @date 2020/4/23
 */
public class ServerActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        TextView textView = findViewById(R.id.id_text_name);
        if (textView != null) {
            FormatUtil.getNetTime(textView::setText);
        }
    }

    public void onClick1(View view) {
        Latte.getConfigurator().withApiHost("https://10.35.11.51:29080");
        RestClient.builder()
                .url("/bai/du/com/")
                .success(LatteLogger::json)
                .failure(() -> LatteLogger.e("ss"))
                .error((code, msg) -> LatteLogger.e(code + "", msg))
                .build()
                .get();
    }

    public void onClick2(View view) {
        Latte.getConfigurator().withApiHost("http://10.35.11.52:29080/");
        RestClient.builder()
                .url("bai/du/com/")
                .success(LatteLogger::json)
                .build()
                .get();
    }

    public void onClick3(View view) {
        final String TOKEN = "Basic c2VydmVyOnNlY3JldA==";
        LattePreference.addCustomAppProfile(UserConstant.AUTHORIZATION, TOKEN);
        RestClient.builder()
                .url("http://10.35.11.66:8084/oauth/token")
                .params("username", "admin")
                .params("password", "password")
                .params("grant_type", "password")
                .params("client_id", "server")
                .params("client_secret", "secret")
                .success(response -> {
                    LatteLogger.json(response);
                    ToastUtils.showShort(response);
                })
                .failure(() -> {
                    LatteLogger.e("msg");
                    ToastUtils.showShort("连接服务器失败，请稍后再试。");
                })
                .error((code, msg) -> {
                    LatteLogger.e("" + msg);
                    ToastUtils.showShort("登录失败，请稍后再试。");
                })
                .build()
                .query();
    }

    public void onClick4(View view) {
        final String TOKEN = "Basic c2VydmVyOnNlY3JldA==";
        LattePreference.addCustomAppProfile(UserConstant.AUTHORIZATION, TOKEN);
        RestClient.builder()
                .url("https://server.gxkelite.com/login?password=Sy93OjPDF5vwNrkTfL9AyA%3D%3D%0A&account=administrator&status=2")
                .success(response -> {
                    LatteLogger.json(response);
                    ToastUtils.showShort(response);
                })
                .failure(() -> {
                    LatteLogger.e("msg");
                    ToastUtils.showShort("连接服务器失败，请稍后再试。");
                })
                .error((code, msg) -> {
                    LatteLogger.e("" + msg);
                    ToastUtils.showShort("登录失败，请稍后再试。");
                })
                .build()
                .queryNo();
    }
}
